<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport"/>
    <link rel="stylesheet" href="css/amazeui.min.css">
    <link rel="stylesheet" href="css/animate.min.css">
    <link rel="stylesheet" href="css/style.css">
    <title>分享社区</title>
</head>
<body>
<header data-am-widget="header"
        class="am-header am-header-default" style="background-color: #FF856E;">
    <div class="am-header-left am-header-nav">
        <a href="javascript:history.go(-1);" style="float: left">
            <i class="am-icon-chevron-left" style="font-size: 20px;color: white;"></i>
        </a>
        <span style="font-size: 18px;color: white;">我的分享</span>
    </div>


    <div class="am-header-right am-header-nav">
        <button class="am-btn am-btn-sm am-btn-link" id="createCommunitySubmit" type="button" style="color: white;">发送</button>
    </div>
</header>

<div class="am-nav" style="width: 96%; margin: 5% auto;">
    <div class="am-u-sm-12" style="margin-bottom: 8%;">
        <textarea placeholder="这一刻你的想法..." id="communityContent" style="width: 100%;background: #f5f5f5f5;height: 100px;max-width: 100%;max-height: 100px;outline: none;color: #606060;"></textarea>
    </div>
    <div class="am-form-group am-u-sm-8">
        <img src="img/lu.jpg" alt="" id="photoCover" class="am-img-thumbnail" style="width: 216px;height: 146px;"/>
    </div>
    <div class="am-form-group am-form-file am-u-sm-4">
        <button type="button" class="am-btn am-btn-warning am-btn-sm"
                style="width: 96px;height: 146px;background: #E3E3E3;border: 0;">
            <i class="am-icon-plus" style="font-size: 36px;color: #606060;"></i>
        </button>
        <input id="file" type="file" name="file">
    </div>
</div>


<script src="js/jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
<script>
    $("#file").change(function (e) {
        var file = e.target.files[0] || e.dataTransfer.files[0];
        $('#photoCover').val(document.getElementById("file").files[0].name);
        if (file) {
            var reader = new FileReader();
            reader.onload = function () {
                $("img").attr("src", this.result);
            }

            reader.readAsDataURL(file);
        }
    });

    $("#createCommunitySubmit").click(function (e) {
        const file = $("#file")[0].files[0];
        const communityContnet  = $("#communityContent").val();
        if (file != null && communityContnet != ""){
            let msg = addCommunity(file, communityContnet);
            if (msg == "success"){
                window.location.href = "community.html"
            }
        }
    })
</script>
</html>